Passed
Push — master ( 2fcfd9...3f2ac7 )
by Dylan
02:37
created

crawler_painter.create   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 1
f 0
cc 2
nc 1
nop 0
1
const crawler_painter = {
2
3
    containers: [],
4
5
    /**
6
     * Create a result table for the provided tests
7
     *
8
     * @param {string} name
9
     * @param {string} title
10
     * @param {Array} headers
11
     * @return {jQuery}
12
     */
13
    create: function(name, title, headers){
14
        var container       = $('<div class="infobox" id="'+name+'"></div>'),
15
            header          = $('<div class="header clearfix"></div>'),
16
            count           = $('<div class="count left"></div>'),
17
            toggle_button   = $('<div class="icon toggle right closed"></div>').hide(),
18
            export_button   = $('<div class="icon export right"></div>').hide(),
19
            title_bar       = $('<h2 class="left">'+title+'</h2>'),
20
            table_cont      = $('<div class="tableCont" style="display:none;"></div>'),
21
            thead           = $('<tr></tr>'),
22
            table           = $('<table class="table table-hover table-condensed"></table>').append(['<thead></thead>', '<tbody></tbody>']);
23
24
        for(var h in headers) thead.append('<th>'+headers[h]+'</th>');
25
        table.find('thead').append(thead);
26
27
        header.append([count, title_bar, toggle_button, export_button]);
28
        table_cont.append(table);
29
        container.append([header, table_cont]);
30
31
        toggle_button.click(function(){
32
            crawler.trigger('TOGGLED', [name]);
33
            var $this   = $(this),
34
                css     = ($this.hasClass('closed')) ? 'opened' : 'closed';
35
            $this.parents('.infobox').find('.tableCont').slideToggle();
36
            $this.removeClass('opened closed');
37
            $this.addClass(css);
38
        });
39
40
        export_button.click(function(){
41
            crawler.trigger('BEFORE_EXPORT', [name]);
42
            var $this       = $(this),
43
                rows        = $this.parents( '.infobox' ).first().find( 'table tr' ),
44
                csvContent  = "data:text/csv;charset=utf-8,";
45
46
            $.each( rows, function(){
47
                var item = [];
48
                $.each( $(this).find( 'th, td' ), function(){ item.push( $(this).text() ); });
49
                csvContent += item.join(',') + "\n";
50
            });
51
52
            var link = document.createElement( 'a' );
53
            link.setAttribute( 'href', encodeURI( csvContent ) );
54
            link.setAttribute( 'download', name + '.csv' );
55
            link.click();
56
            crawler.trigger('AFTER_EXPORT', [name]);
57
        });
58
59
        this.containers.push({'name': name, 'container': container});
60
        return container;
61
    },
62
63
    /**
64
     * Add a row of data to the container which matches
65
     * the name provided
66
     *
67
     * @param {string} name
68
     * @param {Array} data
69
     */
70
    add_row: function(name, data){
71
        var cont    = this.get_container_by_name(name),
72
            table   = cont.find('tbody'),
73
            row     = $('<tr></tr>').appendTo(table),
74
            len     = table.find('tr').length;
75
76
        for(var d in data) row.append($('<td/>').append(data[d]));
77
78
        // Show icons if we have items
79
        if( len > 0 ){
80
            cont.find('.icon.export').fadeIn();
81
            cont.find('.icon.toggle').fadeIn();
82
        }
83
84
        cont.find('.count').html(len);
85
86
        // Set the header colour
87
        if( cont.find('td div.alert-danger').length > 0 ) crawler_painter.set_type(name, 'error');
88
        else if( cont.find('td div.alert-warning').length > 0 ) crawler_painter.set_type(name, 'warning');
89
        else if( cont.find('td div.alert-info').length > 0 ) crawler_painter.set_type(name, 'info');
90
        else if( cont.find('td div.alert-success').length > 0 ) crawler_painter.set_type(name, 'success');
91
    },
92
93
    /**
94
     * Reset the data inside of the table for the container named {name}
95
     *
96
     * @param {string} name
97
     * @param {string|undefined} type
98
     * @return undefined
99
     */
100
    reset_table: function(name, type){
101
        var cont = this.get_container_by_name(name);
102
103
        cont.find('tbody tr').remove();
104
        cont.find('.count').html('');
105
        cont.find('.icon.export').hide();
106
        cont.find('.icon.toggle').hide();
107
108
        if( type != undefined ) this.set_type(name, type);
109
110
        return undefined;
111
    },
112
113
    /**
114
     * Create a status field to be used in the report rows
115
     *
116
     * @param {string} type
117
     * @param {string} text
118
     * @return {jQuery}
119
     */
120
    create_status: function(type, text){
121
        var ret = $('<div class="status-text alert"></div>');
122
        switch(type){
123
            case 'info':
124
                ret.addClass('alert-info');
125
                ret.append('<i class="glyphicon glyphicon-info-sign">&nbsp;</i>');
126
                break;
127
128
            case 'error':
129
                ret.addClass('alert-danger');
130
                ret.append('<i class="glyphicon glyphicon-exclamation-sign">&nbsp;</i>');
131
                break;
132
133
            case 'success':
134
                ret.addClass('alert-success');
135
                ret.append('<i class="glyphicon glyphicon-ok-sign">&nbsp;</i>');
136
                break;
137
138
            case 'warning':
139
                ret.addClass('alert-warning');
140
                ret.append('<i class="glyphicon glyphicon-warning-sign">&nbsp;</i>');
141
                break;
142
            default: return undefined;
143
        }
144
        ret.append(text);
145
        return ret;
146
    },
147
148
    /**
149
     * Return the container matching the provided name
150
     *
151
     * @param {string} name
152
     * @return {jQuery|undefined}
153
     */
154
    get_container_by_name: function(name){
155
        for(var c in this.containers) if(this.containers[c]['name'] == name) return this.containers[c]['container'];
156
        return undefined;
157
    },
158
159
    /**
160
     * Set the type of the test so it's colour changes according
161
     *
162
     * @param {string} name
163
     * @param {string} type
164
     */
165
    set_type: function(name, type){
166
        var cont = this.get_container_by_name(name);
167
        cont.removeClass('blue red green yellow purple');
168
        switch(type){
169
            case 'info': return cont.addClass('blue');
170
            case 'error': return cont.addClass('red');
171
            case 'success': return cont.addClass('green');
172
            case 'warning': return cont.addClass('yellow');
173
            default: return cont.addClass('purple');
174
        }
175
    },
176
177
    /**
178
     * Update the header stats
179
     */
180
    update_header: function(){
181
        $('#leftcount').html(crawler.que.length);
182
        $('#donecount').html(crawler.tested.length);
183
184
        if(crawler.que.length > 0 ) $('#analyzestatus').html('Analyzing');
185
        else if(crawler.que.length < 1 && crawler.tested.length > 0) $('#analyzestatus').html('Finished');
186
    },
187
188
    /**
189
     * Returns a link out of the passed url
190
     *
191
     * @param {string} url
192
     * @returns {string}
193
     */
194
    create_link: function(url, anchor){
195
        anchor = (anchor) ? anchor : url;
196
        return '<a class="btn btn-link" href="'+url+'" title="'+anchor+'" target="_blank" rel="nofollow">'
197
            +'<span class="glyphicon glyphicon-new-window">&nbsp;</span>'+
198
            ((anchor.length > 29) ? anchor.substr(0, 27) + '...' : anchor)
199
            +'</a>';
200
    },
201
202
    /**
203
     * Initialize the painter
204
     */
205
    init:function(){
206
        for(var c in this.containers) $('#results_container').append(this.containers[c]['container']);
207
        crawler.on('CRAWL_FINISHED', this.update_header);
208
    }
209
};
210